// SelectionSort.cpp
#include <vector>
#include <iostream>

using namespace std;

int find_min_idx(int i, vector<int> array) {
  int minValue = array[i];
  int minPos = i;

  // [8, 9, 1, 5, 7]
  //     ^i
  //        ^j
  for (int j = i + 1; j < array.size(); ++j) {
    if (array[j] < minValue) {
      minValue = array[j];
      minPos = j;
    }
  }

  return minPos;
}

// & - 按参数的引用传递
// 默认情况 - 按参数值传递
vector<int> selectionSort(vector<int> array) {
  for (int i = 0; i <= array.size() - 2; ++i) {
    int minPos = find_min_idx(i, array);
    swap(array[i], array[minPos]);
  }

  return array;
}

int main(int argc, char const *argv[])
{
  /* code */
  vector<int> array {8, 9, 1, 6, 7};
  array = selectionSort(array);

  for (int n : array) {
    cout<<n<<" ";
  }
  cout<<endl;
  return 0;
}
